package com.yjc.drygoods.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.TypeReference;
import com.yjc.common.exception.BizCodeEnume;
import com.yjc.common.to.SpuHasStock;
import com.yjc.common.exception.NotWareStockException;
import com.yjc.drygoods.ware.entity.WareInfoEntity;
import com.yjc.drygoods.ware.entity.WareSpuEntity;
import com.yjc.drygoods.ware.feign.ProductFeignService;
import com.yjc.drygoods.ware.service.WareInfoService;
import com.yjc.drygoods.ware.to.SpuInfoTo;
import com.yjc.drygoods.ware.vo.WareLockVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yjc.drygoods.ware.service.WareSpuService;
import com.yjc.common.utils.PageUtils;
import com.yjc.common.utils.R;


/**
 * 商品库存
 *
 * @author yjc
 * @email yjc19979401754@126.com
 * @date 2022-08-19 23:23:36
 */
@RestController
@RequestMapping("ware/warespu")
public class WareSpuController {
    @Autowired
    private WareSpuService wareSpuService;

    @Autowired
    WareInfoService wareInfoService;

    @Autowired
    ProductFeignService productFeignService;



    @PostMapping("/lockStock")
    public R lockStock(@RequestBody WareLockVo wareLockVo) {
        try {
            Boolean lockedRespVos = wareSpuService.lockStock(wareLockVo);
            return R.ok();
        } catch (NotWareStockException e) {
            e.printStackTrace();
            return R.error(BizCodeEnume.NOT_WARE_STOCK.getCodeException(), BizCodeEnume.NOT_WARE_STOCK.getMessageException() + e.getMessage());
        }
    }

    @PostMapping("/hasstock")
    public R getSkuWare(@RequestBody List<Long> skuIds) {
        List<SpuHasStock> skuHasStocks = wareSpuService.getHasStock(skuIds);
        return R.ok().setData(skuHasStocks);
    }

    /**
     * 获取指定spuId商品是否有库存
     *
     * @param spuId
     * @return
     */
    @PostMapping("/single/hasstock")
    public boolean getSingleSkuWare(@RequestBody Long spuId) {
        return wareSpuService.getSingleSpuWare(spuId);
    }

    /**
     * /ware/waresku/list
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = wareSpuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        WareSpuEntity wareSpu = wareSpuService.getById(id);

        //这里改了，原来是wareSku
        return R.ok().put("wareSpu", wareSpu);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody WareSpuEntity wareSpu) {
        Long spuId = wareSpu.getSpuId();
        Long wareId = wareSpu.getWareId();
        Double stock = wareSpu.getStock();
        wareSpuService.updateWareStock(spuId, wareId, stock);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody WareSpuEntity wareSpu) {
        WareInfoEntity wareInfo = wareInfoService.getById(wareSpu.getWareId());
        R r = productFeignService.spuInfo(wareSpu.getSpuId());
        if (r.getCode() != 0) {
            throw new RuntimeException("干货远程服务调用异常");
        }
        SpuInfoTo data = r.getData("spuInfo", new TypeReference<SpuInfoTo>() {
        });
        wareSpu.setWareName(wareInfo.getName());
        wareSpu.setSpuName(data.getSpuName());
        wareSpuService.updateById(wareSpu);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        wareSpuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
